# @Time : 2024/10/30 22:36
# @Author : Fioman 
# @Phone : 13149920693
"""
is 和 == 的区别
is: 比较两个对象是否是同一个对象,就是本质上比较的是id(a) == id(b)
==: 本质上是调用了对象的魔法方法__eq__, 大部分的时候我们是比较的两个对象的值,但是如果
      你没有实现自己的__eq__比较方法,继承了object类的话,还是比较的id(a) == id(b)
"""
a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(a is b)  # True
print(a is c)  # False

print(a == b)  # True
print(a == c)  # True


class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age


p1 = Person("Fioman", 12)
p2 = Person("Fioman", 12)
print(p1 is p2)
print(p1 == p2)  # False 没有实现__eq__方法,默认是比较id(p1)==id(p2)


class Person2:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def __eq__(self, other):
        print("Person2.__eq__() 调用")
        if isinstance(other, Person2):
            return self.name == other.name and self.age == other.age
        return False


p1 = Person2("Fioman", 12)
p2 = Person2("Fioman", 12)
print(p1 is p2)
print(p1 == p2)  # True,重载了__eq__()方法,所以会调用自己定义的__eq__()方法
